package com.common.picker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.common.R;

import java.util.Arrays;
import java.util.List;


public class SinglePickUtil extends Dialog {

    public static interface OnSelectedListener {
        void onSelected(Object selectedValue);
    }

    public static interface OnSelectedPositionListener {
        void onSelected(Object selectedValue, int position);
    }

    private OnSelectedListener mOnSelectedListener;
    private OnSelectedPositionListener mOnSelectedPositionListener;
    private WheelPicker mWheelPicker;
    private TextView mTvSure, mTvTitle, mTvCancel;
    private CharSequence mTitle;
    private List<?> mData;
    private Object mCurrentSelectedData;
    private int mPosition = 0;

    public SinglePickUtil(Context context) {
        super(context, R.style.DialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_picker);
//        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//去掉动画,防止连续弹出两次对话框会出现闪屏现象
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
//        layoutParams.y = BarUtils.isNavBarVisible(getWindow()) ? BarUtils.getStatusBarHeight() : 0;
        layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        mTvSure = findViewById(R.id.tv_sure);
        mTvCancel = findViewById(R.id.tv_cancel);
        mTvTitle = findViewById(R.id.tv_title);
        mWheelPicker = findViewById(R.id.wheelpicker);

        resetData(mData);
        setTitle(mTitle);

        initListener();
    }

    @Override
    public void setTitle(@Nullable CharSequence title) {
        super.setTitle(title);
        mTitle = title;
        if (mTvTitle != null) mTvTitle.setText(title);
    }

    public void selectItemByPosition(int position) {
        if (mWheelPicker != null) {
            mWheelPicker.setSelectedItemPosition(position);
        }
        if (mData != null && !mData.isEmpty()) {
            mCurrentSelectedData = mData.get(position);
            mPosition = position;
        }
    }

    public void resetData(List<?> data) {
        mData = data;
        if (mWheelPicker != null) mWheelPicker.setData(mData);
        if (mData != null && !mData.isEmpty()) {
            mCurrentSelectedData = mData.get(0);
            mPosition = 0;
        }
    }

    public void resetData(String[] data) {
        mData = Arrays.asList(data);
        if (mWheelPicker != null) mWheelPicker.setData(mData);
        if (mData != null && !mData.isEmpty()) {
            mCurrentSelectedData = mData.get(0);
            mPosition = 0;
        }
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        mOnSelectedListener = onSelectedListener;
    }

    public void setOnSelectedPositionListener(OnSelectedPositionListener onSelectedPositionListener) {
        mOnSelectedPositionListener = onSelectedPositionListener;
    }

    private void initListener() {
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSelectedListener != null) {
                    mOnSelectedListener.onSelected(mCurrentSelectedData);
                }
                if (mOnSelectedPositionListener != null) {
                    mOnSelectedPositionListener.onSelected(mCurrentSelectedData, mPosition);
                }
                dismiss();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        mWheelPicker.setOnItemSelectedListener(new IWheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                mCurrentSelectedData = data;
                mPosition = position;
            }
        });
    }

    public void setSelectedItem(int position) {
        mWheelPicker.setSelectedItemPosition(position);
        if (mData != null && !mData.isEmpty()) {
            mCurrentSelectedData = mData.get(position);
            mPosition = position;
        }
    }

    public void setSelectItem(int position) {
        if (mData == null) {
            return;
        }
        if (mData.size() < position) {
            mPosition = 0;
            return;
        }
        mPosition = position;
        mCurrentSelectedData = mData.get(position);
        if (mWheelPicker != null) {
            mWheelPicker.setData(mData);
            mWheelPicker.setSelectedItemPosition(mPosition);
        }
    }

}
